﻿using OrderSystem.Common;
using OrderSystem.DataAccess;
using OrderSystem.Templet;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace OrderSystem
{
    public partial class FormOrderDetailForTempered : FormTemplet
    {
        private OrderCondition _orderCondition = null;

        private DataTable _dtDetail = null;

        public FormOrderDetailForTempered()
        {
            this.InitializeComponent();
        }

        public FormOrderDetailForTempered(OrderCondition oc) : this()
        {
            this._orderCondition = oc;
            Orders orders = new Orders();
            try
            {
                this._dtDetail = orders.GetOrderDetailForTempered(this._orderCondition);
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message + "\r\n\r\n或者是连接数据库的通道出现故障，请检查！\r\n\r\n订单信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            catch (Exception ex2)
            {
                MessageBox.Show(ex2.Message + "\r\n\r\n订单信息可能没有读出来", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            orders.Dispose();
            if (this._dtDetail != null)
            {
                if (this._dtDetail.Rows.Count > 0)
                {
                    this.dgDetail.DataSource = null;
                    this.dgDetail.DataSource = this._dtDetail;
                }
                else
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n订单信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    this.dgDetail.DataSource = null;
                    base.Dispose();
                }
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                string text = this.dgDetail.Rows[0].Cells[0].Value.ToString();
            }
            catch (Exception)
            {
                MessageBox.Show("表格中没有数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            int rowIndex = this.dgDetail.CurrentCell.RowIndex;
            new FormOrderInfoForTempered(this.dgDetail.Rows[rowIndex].Cells[1].Value.ToString())
            {
                MdiParent = base.MdiParent
            }.Show();
        }

        private void dgDetail_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0 && e.RowIndex <= this.dgDetail.Rows.Count - 1)
            {
                new FormOrderInfoForTempered(this.dgDetail.Rows[e.RowIndex].Cells[1].Value.ToString())
                {
                    MdiParent = base.MdiParent
                }.Show();
            }
        }

        private void FormOrderDetailForTempered_Load(object sender, EventArgs e)
        {
            this.dgDetail.AutoResizeColumns();
            this.dgDetail.Columns["已钢化"].HeaderText = "已完成";
        }

        private void btnStatistics_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dgDetail.Rows.Count; i++)
            {
                if (Convert.ToInt32(this.dgDetail.Rows[i].Cells["总数量"].Value) == Convert.ToInt32(this.dgDetail.Rows[i].Cells["已钢化"].Value))
                {
                    this.dgDetail.Rows[i].DefaultCellStyle.BackColor = Color.LawnGreen;
                }
                if (Convert.ToInt32(this.dgDetail.Rows[i].Cells["总数量"].Value) < Convert.ToInt32(this.dgDetail.Rows[i].Cells["已钢化"].Value))
                {
                    this.dgDetail.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                }
                if (Convert.ToInt32(this.dgDetail.Rows[i].Cells["总数量"].Value) > Convert.ToInt32(this.dgDetail.Rows[i].Cells["已钢化"].Value) && Convert.ToInt32(this.dgDetail.Rows[i].Cells["已钢化"].Value) > 0)
                {
                    this.dgDetail.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
                }
            }
            if (this._dtDetail != null)
            {
                if (this._dtDetail.Rows.Count > 0)
                {
                    double num = 0.0;
                    double num2 = 0.0;
                    double num3 = 0.0;
                    double num4 = 0.0;
                    for (int i = 0; i < this._dtDetail.Rows.Count; i++)
                    {
                        num += (double)Convert.ToInt32(this._dtDetail.Rows[i][6]);
                        num3 += (double)Convert.ToInt32(this._dtDetail.Rows[i][7]);
                        num2 += Convert.ToDouble(this._dtDetail.Rows[i][8]);
                        num4 += Convert.ToDouble(this._dtDetail.Rows[i][9]);
                    }
                    MessageBox.Show(string.Concat(new string[]
                    {
                        "总数量为：",
                        num.ToString(),
                        "；已钢化数量：",
                        num3.ToString(),
                        "\r\n总面积为：",
                        num2.ToString(),
                        "；已钢化面积为：",
                        num4.ToString()
                    }), "统计", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    MessageBox.Show("根据你的信息，在系统中没有找到你要得信息\r\n\r\n信息可能没有读出来", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            base.Close();
        }
    }
}
